<template>
	<view>
		<view class="login">
			<image src='https://xt-ylsj.oss-cn-shenzhen.aliyuncs.com/xcx_sxy_img/xyx_logo.png' class='logos'></image>
		</view>

		<view class='titles'>申请获得以下权限</view>
		<view class='stipss'>·获得您的公开信息(昵称、头像等)</view>
		<!-- 需要使用 button 来授权登录 -->
		<button open-type="getUserInfo" @click="bindGetUserInfo">登录</button>

	</view>
</template>

<script>
	import {
		wxLogin
	} from '@/config/api.js';
	import {
		globalData
	} from '@/common/globaldata.js'
	export default {
		data() {
			return {
				tapTime: ''
			}
		},
		methods: {
			bindGetUserInfo(e) {

				wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						uni.showLoading({
							title: '登录中',
						})
						let user = res.userInfo
						uni.login({
							success(log) {
								let data = {
									code: log.code,
									encryptedData: res.encryptedData,
									iv: res.iv,
									sysCode: 102,
									appId: globalData.appId,
									loginType: 'xcx',
									nickName: user.nickName,
									gender: user.gender,
									avatarUrl: user.avatarUrl,
								}
								console.log(data)
								wxLogin(data).then(res => {
									uni.showToast({
										title:'登录成功',
										icon:'none'
									})
									uni.hideLoading()
									console.log(res)
									uni.setStorageSync('userInfo', res)
									uni.setStorageSync('token', res.token)
									if (uni.getStorageSync('url')) {

										uni.redirectTo({ //非tabber
											url: uni.getStorageSync('url'),
											fail(err) {
												console.log(err, 'err')
											}
										})
										// uni.removeStorageSync('url')
									} else if (uni.getStorageSync('urls')) { //tabber跳转
										uni.switchTab({
											url: uni.getStorageSync('urls')
										})
										uni.removeStorageSync('urls')
									} else {
										uni.redirectTo({
											url: '/pages/home/home/home',
											fail(err) {
												console.log(err, 'err')
											}
										})
									}
								})
							}
						})

						// this.setData({
						//   userInfo: res.userInfo,
						//   hasUserInfo: true
						// })
					},
				})
				var that = this;
				var nowTime = new Date();
				if (nowTime - this.tapTime < 800) {
					console.log('阻断')
					return;
				}

				// 	uni.getSetting({
				// 		success(res) {

				// 			if (res.authSetting['scope.userInfo']) {
				//                     var iv = '';
				// 					var encryptedData = '';
				// 				uni.login({
				// 					success: function(res) {
				// 						uni.getUserInfo({
				// 							success: function(res) {
				// 								console.log(res)
				// 								iv = res.iv
				// 								encryptedData = res.encryptedData


				// 							}
				// 						})
				// 						uni.setStorage({
				// 							key: 'code',
				// 							data: res.code,
				// 						})
				// 						if (res.code) {

				// 							setTimeout(function() {
				// 								let data = {
				// 									code: res.code,
				// 									encryptedData: encryptedData,
				// 									iv: iv
				// 								}
				// 								that.$u.post('login/xcxlogin', data)
				// 									.then(res => {
				// 										console.log(res)
				// 										that.$store.commit('changeUserinfo',res)
				// 										uni.setStorageSync('userInfo',res)
				// 										uni.setStorageSync('token',res.token)
				// 										//判断从哪跳过来的
				// 										if(uni.getStorageSync('url')){

				// 											uni.redirectTo({ //非tabber
				// 												url:uni.getStorageSync('url')
				// 											})
				// 											uni.removeStorageSync('url')
				// 										}else if(uni.getStorageSync('urls')){ //tabber跳转
				// 											uni.switchTab({
				// 												url:uni.getStorageSync('urls')
				// 											})
				// 											uni.removeStorageSync('urls')
				// 										}else{
				// 											uni.switchTab({
				// 												url:'/pages/tabBer/course/course'
				// 											})
				// 										}
				// 										// res为服务端返回的数据
				// 									})

				// 							}, 800)
				// 						}
				// 					}
				// 				});
				// 			} else {
				// 				uni.showToast({
				// 					title: res.data.msg,
				// 					icon: 'none'
				// 				})


				// 			}
				// 		}
				// 	})

				that.tapTime = nowTime

			},
		}
	}
</script>

<style>
	/* pages/login/login.wxss */
	.header {
		margin: 90rpx 0 90rpx 50rpx;
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 300rpx;
		line-height: 450rpx;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
	}

	.login {
		width: 100%;
		text-align: center;
		margin: 150rpx 0;
	}

	.logos {
		display: inline-block;
		width: 150rpx;
		height: 150rpx;
	}

	.titles {
		width: 100%;
		font-size: 30rpx;
		color: #000;
		padding: 21rpx 44rpx 26rpx 44rpx;
		box-sizing: border-box;
		letter-spacing: 2rpx;
	}

	.stipss {
		font-size: 24rpx;
		color: #808080;
		padding: 0rpx 45rpx 96rpx 45rpx;
		box-sizing: border-box;
		letter-spacing: 2rpx;
	}

	button {
		width: calc(100% - 90rpx) !important;
		height: 80rpx !important;
		margin-left: 45rpx;
		line-height: 80rpx !important;
		border-radius: 78rpx;
		color: white;
		background-color: #06C15F;
		padding: 0;
		font-size: 30rpx;
	}

	/* button:active{
    background-color: #fe5ba9;
    color: white;
} */
</style>
